package com.ruizhong.integratedmediawithoutc.util;

import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;

public class SRSUtil {

    public static String srsServer_ip="192.168.0.62";

    public static String getSrsServer_ip() {
        return srsServer_ip;
    }

    public static void setSrsServer_ip(String srsServer_ip) {
        SRSUtil.srsServer_ip = srsServer_ip;
    }

    public static Map<String, Object> getStreams() {
        String urlString = "http://"+srsServer_ip+":1985/api/v1/streams";
        HttpURLConnection connection = null;
        BufferedReader reader = null;

        try {
            // 创建URL对象
            URL url = new URL(urlString);

            // 打开连接
            connection = (HttpURLConnection) url.openConnection();

            // 设置请求方法为GET
            connection.setRequestMethod("GET");

            // 获取响应码
            int responseCode = connection.getResponseCode();
            System.out.println("响应码: " + responseCode);

            // 如果响应码为200（HTTP_OK），则读取响应内容
            if (responseCode == HttpURLConnection.HTTP_OK) {
                reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                StringBuilder response = new StringBuilder();
                String line;

                while ((line = reader.readLine()) != null) {
                    response.append(line);
                }

                // 将响应字符串解析为JSON对象（假设响应是JSON格式）
                ObjectMapper objectMapper = new ObjectMapper();
                Map<String, Object> responseMap = objectMapper.readValue(response.toString(), Map.class);

                // 打印结果

                return responseMap;
            } else {
                System.out.println("请求失败，响应码: " + responseCode);
                return null;
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭资源
            try {
                if (reader != null) {
                    reader.close();
                }
                if (connection != null) {
                    connection.disconnect();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public static void kickStream(String srsServer, String cid) throws IOException {
        String apiUrl = String.format("%s/api/v1/clients/%s", srsServer, cid);
        URL url = new URL(apiUrl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("DELETE");

        int responseCode = connection.getResponseCode();
        if (responseCode == HttpURLConnection.HTTP_OK) {
            System.out.println("成功踢出流: " + cid);
        } else {
            System.out.println("踢出流失败，响应码: " + responseCode);
        }

        connection.disconnect();
    }

}
